<?php

/**
 * Plugin Name: Blog filter widget
 * Description: Blog filter widget
 * Version: 0.2
 * Author: Johan Sall Larsson
 * Author URI: http://www.johansalllarsson.se
*/

	include_once( WP_PLUGIN_DIR . "/blog-filter/blog-filter.php" );

	add_action('widgets_init', create_function('', 'return register_widget("Blog_filter_widget");'));
	
	class Blog_filter_widget extends WP_Widget {
	  
		/** constructor */
		function Blog_filter_widget() {
			parent::WP_Widget(false, $name = 'Blog filter');
		}

		/** @see WP_Widget::update */
		function update($new_instance, $old_instance) {
			$instance = $old_instance;
			$instance['title'] = strip_tags($new_instance['title']);
			$instance['filter'] = strip_tags($new_instance['filter']);
			$instance['posts_per_page'] = (int)strip_tags($new_instance['posts_per_page']);
			return $instance;
		}

		/** @see WP_Widget::form */
		function form($instance) {
		
			$current_filters = get_option('blog_filter_settings');
			$title = esc_attr($instance['title']);
			$filter = esc_attr($instance['filter']);
			$posts_per_page = esc_attr($instance['posts_per_page']);
			$posts_per_page = ( $posts_per_page == 0 ) ? '' : $posts_per_page;
			if ( !empty($filter) ) {
				$posts_per_page = $current_filters[$filter]['p'];
			}
			?>
			
			<p>
			  <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> 
			  <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
			</p>
			
			<p>
				<label for="<?php echo $this->get_field_id('filter'); ?>"><?php _e('Filter') ?>
					<select id="<?php echo $this->get_field_id('filter'); ?>" name="<?php echo $this->get_field_name('filter'); ?>">
					<?php foreach ( $current_filters as $value ) { ?>
						<option <?php echo ( $filter == $value['n'] ) ? 'selected="selected"' : ''; ?> value="<?php echo $value['n']; ?>"><?php echo $value['n']; ?></option>
					<?php } ?>
					</select>
				</label>
			</p>
			
			<p>
			  <label for="<?php echo $this->get_field_id('posts_per_page'); ?>"><?php _e('Amount of posts to show:'); ?></label> 
			  <input class="widefat" id="<?php echo $this->get_field_id('posts_per_page'); ?>" name="<?php echo $this->get_field_name('posts_per_page'); ?>" type="text" value="<?php echo $posts_per_page; ?>" />
			</p>

			<?php 
		}
		
		/** @see WP_Widget::widget */
		function widget($args, $instance) {
			extract( $args );
			$title = apply_filters('widget_title', $instance['title']);
			$filter = apply_filters('widget_filter', $instance['filter']);
			$posts_per_page = apply_filters('widget_filter', $instance['posts_per_page']);
			echo $before_widget;
			echo '<div class="filter-'.$filter.'">';
			if ( $title ) {
				echo $before_title . $title . $after_title;
			}
			if ( !$posts_per_page > 0 ) {
				$posts_per_page = null;
			}
			echo blog_filter_func( array( 'filter_name' => $filter, 'tpl' => true, 'posts_per_page' => $posts_per_page, 'paging' => true ) );
			echo '</div>';
			echo $after_widget;
		}
	  
	}

?>